package br.mia.unifor.onaga.cloud;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import br.mia.unifor.onaga.cloud.artifact.Appliance;
import br.mia.unifor.onaga.cloud.artifact.Application;
import br.mia.unifor.onaga.cloud.artifact.Instance;
import br.mia.unifor.onaga.cloud.artifact.InstanceType;
import br.mia.unifor.onaga.cloud.artifact.InstanceTypeCapacityLevels;
import br.mia.unifor.onaga.cloud.artifact.Metric;
import br.mia.unifor.onaga.cloud.artifact.Provider;
import br.mia.unifor.onaga.cloud.artifact.Scenario;
import br.mia.unifor.onaga.cloud.artifact.Workload;
import br.mia.unifor.onaga.cloud.artifact.script.ScriptParser;
import br.mia.unifor.onaga.cloud.artifact.script.Scriptlet;


import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;


public class YamlLoader {
	public static Test loadTest(InputStream in) throws YamlException{
		YamlReader reader = new YamlReader(new InputStreamReader(in));
		reader.getConfig().setClassTag("instanceType",InstanceType.class);
		reader.getConfig().setClassTag("instance",Instance.class);
		reader.getConfig().setClassTag("metric",Metric.class);
		reader.getConfig().setClassTag("instanceTypeCapacityLevel",InstanceTypeCapacityLevels.class);
		reader.getConfig().setClassTag("workload",Workload.class);
		reader.getConfig().setClassTag("scenario",Scenario.class);
		reader.getConfig().setClassTag("provider",Provider.class);
		reader.getConfig().setClassTag("scriptlet",Scriptlet.class);
		reader.getConfig().setClassTag("application",Application.class);
		
		reader.getConfig().setClassTag("test",Test.class);
		//reader.getConfig().setPropertyElementType(InstanceTypeClass.class, "instanceTypes", InstanceType.class);
		//reader.getConfig().setPropertyElementType(br.unifor.ow2.onaga.cloud.model.Test.class, "scenarios", br.unifor.ow2.onaga.cloud.model.Scenario.class);
		//reader.getConfig().setPropertyElementType(br.unifor.ow2.onaga.cloud.model.Scenario.class, "instances", br.unifor.ow2.onaga.cloud.model.Instance.class);
		
		
		Test test = (Test) reader.read();
		
		return test;
	}
	
	public static void main(String[] args) {
		try {
			
			Test test = loadTest(YamlLoader.class.getClass().getResourceAsStream(
			"/br/template.yml"));
			
			Appliance appliance = test.getScenarios().get(0).getAppliance();
			
			Instance instance = appliance.getInstances().get(2);
			
			ScriptParser.parse(appliance, instance.getOnStartup().get(0), instance);
			
			System.out.println(test);
			
			
		} catch (YamlException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}